package cn.lichenfei.jfx.music;

import cn.hutool.http.HttpUtil;
import cn.lichenfei.jfx.music.common.Config;
import cn.lichenfei.jfx.music.gui.MusicPane;
import dorkbox.systemTray.Menu;
import dorkbox.systemTray.MenuItem;
import dorkbox.systemTray.SystemTray;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * @author ChenFei
 * @date 2022年4月25日10:42:10
 */
public class Application extends javafx.application.Application {

    private double xOffset;
    private double yOffset;
    private static SystemTray systemTray;
    //
    private static final int port = 34564;
    private static final String requestUrl = "http://127.0.0.1:" + port;

    @Override
    public void start(Stage stage) {
        appStart(stage);//启动程序
        MusicPane musicPane = new MusicPane();
        Scene scene = new Scene(musicPane);
        //鼠标箭头样式
        //scene.setCursor(Cursor.cursor(getClass().getResource("img/j.png").toExternalForm()));
        scene.setFill(null);
        //不显示标题栏
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setTitle("CF-音乐播放器");
        //设置窗口图标
        stage.getIcons().add(new Image(Application.class.getResourceAsStream("img/icon.png")));
        stage.setScene(scene);
        //窗口大小不可变
        stage.setResizable(false);
        stage.show();
        //
        extracted(stage, musicPane);
        //系统托盘
        Platform.setImplicitExit(false);//关闭窗口不退出程序
        systemTray = SystemTray.get();
        systemTray.setTooltip("CF-音乐播放器");
        systemTray.setImage(Application.class.getResourceAsStream("img/icon.png"));
        MenuItem exit = new MenuItem();//退出
        exit.setText("退出");
        exit.setCallback(e -> Platform.runLater(() -> {
            Config.storeSetting();
            //Platform.setImplicitExit(true);
            //stage.close();
            //systemTray.shutdown();
            System.exit(0);
        }));
        MenuItem show = new MenuItem();//显示
        show.setText("打开");
        show.setCallback(e -> Platform.runLater(() -> stage.show()));
        Menu menu = systemTray.getMenu();
        menu.add(show);
        menu.add(exit);
    }

    /**
     * 窗口移动
     *
     * @param stage
     * @param musicPane
     */
    private void extracted(Stage stage, MusicPane musicPane) {
        //窗口移动
        musicPane.setOnMousePressed(event -> {
            event.consume();
            xOffset = stage.getX() - event.getScreenX();
            yOffset = stage.getY() - event.getScreenY();
        });
        musicPane.setOnMouseDragged(event -> {
            event.consume();
            stage.setX(event.getScreenX() + xOffset);
            stage.setY(event.getScreenY() + yOffset);
        });
    }

    public static void main(String[] args) {
        launch();
    }

    /**
     * 程序启动
     *
     * @param stage
     */
    private void appStart(Stage stage) {
        try {
            HttpUtil.createServer(port)
                    .addAction("/", (request, response) -> {
                        /**
                         * 检测到程序二次启动，打开目前的程序
                         */
                        response.write("ok");
                        Platform.runLater(() -> {
                            stage.show();
                            stage.setIconified(false);
                        });
                    })
                    .start();
        } catch (Exception e) {
            e.printStackTrace();
            HttpUtil.get(requestUrl);
            System.exit(0);//二次启动退出程序
        }
    }
}